# 特征工程

# 导入工具包
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

# 1、读入数据
dpath = "E:/VC_project/data/diabetes/"
train = pd.read_csv(dpath+"pima-indians-diabetes.csv")
print(train.info())

# 2、对于缺失值较多的特征，新增一个特征，表示特征是否缺失
# 用NaN代替0，表示缺失值
NaN_col_names = ["Plasma_glucose_concentration","blood_pressure",
                 "Triceps_skin_fold_thickness","serum_insulin","BMI"]
train[NaN_col_names] = train[NaN_col_names].replace(0,np.NaN)
print(train.isnull().sum())
# 肱三头肌皮褶厚度 、餐后血清胰岛素中缺失值较多
feat_miss = ["Triceps_skin_fold_thickness","serum_insulin"]

for i in feat_miss:
    train[i+"_Missing"] = train[i].apply(lambda x:1 if pd.isnull(x) else 0)
    print(train[[i,i+"_Missing"]].head(10))

    sns.countplot(x=i+"_Missing",hue="Target",data=train)
    plt.show()


# 结果显示：特征是否缺失好像和目标关系不大，因此删除新增特征
train.drop(["Triceps_skin_fold_thickness_Missing","serum_insulin_Missing"],axis=1,inplace=True)

# 3、中值填充缺失值
medians = train.median()
train = train.fillna(medians)

print(train.isnull().sum())

# 4、数据标准化处理
from sklearn.preprocessing import StandardScaler

y_train = train["Target"]
X_train = train.drop(["Target"],axis=1)

feat_names = X_train.columns

ss = StandardScaler()
X_train = ss.fit_transform(X_train)

X_train = pd.DataFrame(columns=feat_names,data=X_train)
train = pd.concat([X_train,y_train],axis=1)

train.to_csv(dpath+"FE_diabets.csv",index=False,header=True)
print(train.head())